package starsservice.com.kc.service.impl;

import com.nimbusds.jose.JOSEException;
import common.common.Util.JWTutil;
import common.common.Util.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import starsservice.com.kc.Util.PasswordUtil;
import starsservice.com.kc.Util.RedisUtils;
import starsservice.com.kc.bean.Account;
import starsservice.com.kc.bean.AccountPassowrd;
import starsservice.com.kc.bean.Groud;
import starsservice.com.kc.service.IAccountPassowrdService;
import starsservice.com.kc.service.IAccountService;
import starsservice.com.kc.service.IGroudService;
import starsservice.com.kc.service.RegistAndLoginService;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class RegistAndLoginServiceImpl implements RegistAndLoginService {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private IAccountService accountService;

    @Resource
    private IAccountPassowrdService passowrdService;

    @Resource
    private IGroudService groudService;


    @Override
    public ResponseBean LoginPhone(String phone, String password) {
        Map<String,Object> map = new HashMap<>();
        ResponseBean responseBean = null;

        try {
            String newpassowrd = PasswordUtil.encryptAES(password.getBytes());
            AccountPassowrd accountPassowrd = passowrdService.selectByphone(phone,newpassowrd);

            if (phone.equals(accountPassowrd.getPhone())&&newpassowrd.equals(accountPassowrd.getAccountPassowrd())){
                Account account = accountService.selectByphone(phone);
                String token = this.createToken(account);
                map.put("token",token);
                return new ResponseBean(200,"success",map);
            }
        }catch (NoSuchAlgorithmException | InvalidKeyException
                | InvalidAlgorithmParameterException | NoSuchPaddingException
                | BadPaddingException | IllegalBlockSizeException e) {
            log.error("密码校验失败"+e.getMessage());
            e.printStackTrace();
            return new ResponseBean(400,"密码错误请重试",null);
        }

        return new ResponseBean(401,"登陆失败请重试",null);
    }

    //注册接口
    @Override
    public ResponseBean regist(Account account, String code, String Password) {
        String msgcode = (String) redisUtils.get(account.getAccountPhone());
        if (!msgcode.equals(code)){
            return new ResponseBean(400,"验证码错误",null);
        }
        try {

            int i = accountService.InsterAccount(account);
            if (i>0){
                passowrdService.InsterAccountPassword(Password,account);
                return new ResponseBean(200,"注册成功",null);
            }

        } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseBean(500,"服务器制作密码出错",null);
        }catch (DataIntegrityViolationException e){
            log.error("注册失败:"+e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseBean(400,"注册失败请重试查看资料是否填写完毕",null);
        }
        return new ResponseBean(400,"注册失败请重试",null);
    }

    @Override
    public ResponseBean LoginJoBnumber(String jobnumber, String password) {
        Map<String,Object> map = new HashMap<>();
        ResponseBean responseBean = null;

        try {
            String newpassowrd = PasswordUtil.encryptAES(password.getBytes());
            AccountPassowrd accountPassowrd = passowrdService.selectByjobnumber(jobnumber,newpassowrd);

            if (jobnumber.equals(accountPassowrd.getPhone())&&newpassowrd.equals(accountPassowrd.getAccountPassowrd())){
                Account account = accountService.selectByjobnumber(jobnumber);
                String token = this.createToken(account);
                map.put("token",token);
                return new ResponseBean(200,"登陆成功",map);
            }
        } catch (NoSuchAlgorithmException | InvalidKeyException
                | InvalidAlgorithmParameterException | NoSuchPaddingException
                | BadPaddingException | IllegalBlockSizeException e) {
            log.error("密码校验失败"+e.getMessage());
            e.printStackTrace();
            return new ResponseBean(402,"密码错误请重试",null);
        }

        return new ResponseBean(403,"登陆失败请重试",null);
    }

    /**
     * 登陆公共内容处理
     * @param account
     * @return
     */
    @Override
    public String createToken(Account account) {
        if (account==null){
            return null;
        }
        String token = "";
        Groud groud = groudService.selectByGroudId(account.getAccountId());
        try {
            token = (String) JWTutil.craateToken(groud.getAuthority()
                    , account.getAccountId(), account.getAccountJobNumber());
            redisUtils.set(account.getAccountId(),token);
            return token;
        } catch (JOSEException e) {
            log.error("token制作失败了"+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}
